<template> 
	<el-card class="form-container" shadow="never">
		<el-form :model="memberLevel" :rules="rules" ref="memberLevelFrom" label-width="150px">

			<el-form-item label="等级名称：" prop="level">
				<el-input v-model="memberLevel.level"></el-input>
			</el-form-item>
			<el-form-item label="积分数量：" prop="score">
				<el-input v-model="memberLevel.score"></el-input>
			</el-form-item>
			<el-button type="primary" @click="onSubmit('memberLevelFrom')">提交</el-button>
			<el-button v-if="!isEdit" @click="resetForm('memberLevelFrom')">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>
<script>
	import {
		createMemberLevel,
		getMemberLevel,
		editLevel
	} from '@/api/memberLevel'
	import SingleUpload from '@/components/Upload/singleUpload'
	import {
		formatDate
	} from '@/utils/date';
	const defaultMemberLevel = {};
	export default {
		name: 'MemberLevelDetail',
		components: {
			SingleUpload
		},
		props: {
			isEdit: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				memberLevel: Object.assign({}, defaultMemberLevel),
				rules: {}
			}
		},
		created() {
			if (this.isEdit) {
				getMemberLevel(this.$route.query.id).then(response => {
					console.log(response)
					this.memberLevel = response.data;
					console.log(this.memberLevel)
				});
			} else {
				this.memberLevel = Object.assign({}, defaultMemberLevel);
			}
		},
		methods: {
			onSubmit(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.$confirm('是否提交数据', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning'
						}).then(() => {
							if (this.isEdit) {
								console.log(this.memberLevel)
								editLevel(this.memberLevel).then(response => {
									if (response.code == 200) {
										this.$refs[formName].resetFields();
										this.$message({
											message: '修改成功',
											type: 'success',
											duration: 1000
										});
										this.$router.back();
									} else {
										this.$message({
											message: response.msg,
											type: 'error',
											duration: 1000
										});
									}

								});
							} else {
								createMemberLevel(this.memberLevel).then(response => {
									if (response.code == 200) {
										this.$refs[formName].resetFields();
										this.memberLevel = Object.assign({}, defaultMemberLevel);
										this.$message({
											message: '提交成功',
											type: 'success',
											duration: 1000
										});
										this.$router.back();
									} else {
										this.$message({
											message: response.msg,
											type: 'error',
											duration: 1000
										});
									}

								});
							}
						});

					} else {
						this.$message({
							message: '验证失败',
							type: 'error',
							duration: 1000
						});
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
				this.memberLevel = Object.assign({}, defaultMemberLevel);
			}
		}
	}
</script>
<style>
</style>
